package com.liyi.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liyi.service.DepService;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.Dep;

@Controller("depControllerLiyi")
@RequestMapping("depliyi")
public class DepController {
	
	@Inject
	private DepService depServiceLiyi;
	
	/* 获取 所有部门 列表 */
	@RequestMapping("/get")
	@ResponseBody
	public Object get(HttpServletResponse response, HttpSession session) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			List<Dep> deps = depServiceLiyi.getAllDeps();
			 ArrayNode re = JsonCreator.generateJsonArray();
			for (Dep dep : deps) {
				ObjectNode node = JsonCreator.generateJsonObj();
				node.put("dep_id", dep.getDepId());
				node.put("dep_name", dep.getDepName());
				re.add(node);
			}
			// 封装所有员工的工资项和服务器响应信息
			objectNode.putPOJO("response", re);
			objectNode.put("message", "获取该部门下所有员工工资成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			// System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}
}
